﻿using EPiServer.Enterprise.Mirroring;

namespace ElencySolutions.MultipleProperty
{
    using System;
    using EPiServer.Core;
    using EPiServer.Core.Transfer;
    using EPiServer.Framework;
    using EPiServer.Framework.Initialization;
    
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class MultiplePropertyMirroringModule : IInitializableModule 
    {
        public void Initialize(InitializationEngine context)
        {
            //MirroringDataImporter.ImportPropertyEvent += MirroringDataImporterImportPropertyEvent;
            //MirroringDataExporter.ExportPropertyEvent += MirroringDataExporterExportPropertyEvent;
        }

        private void MirroringDataExporterExportPropertyEvent(object sender, TransformPropertyEventArgs e)
        {
            IPageTransferContext pageTransferContext = sender as IPageTransferContext;

            if (string.IsNullOrEmpty(e.PropertySource.TypeName))
                return;

            Type type = Type.GetType(e.PropertySource.TypeName);

            if (pageTransferContext == null || e == null || e.IsHandled || e.PropertySource == null || e.PropertySource.Type != PropertyDataType.LongString ||
                type == null || type.BaseType == null || !type.BaseType.FullName.Contains("ElencySolutions.MultipleProperty.MultiplePropertyBase"))
            {
                return;
            }

            PropertyData instance = Activator.CreateInstance(type) as PropertyData;
            instance.LoadData(e.PropertySource.Value);

            IMirroringHandlerProperty mirroringHandlerProperty = instance as IMirroringHandlerProperty;

            if (mirroringHandlerProperty == null)
                return;

            mirroringHandlerProperty.HandleMirroring(pageTransferContext, e, false);
            e.PropertySource.Value = instance.Value.ToString();
            e.IsHandled = true;
        }

        private void MirroringDataImporterImportPropertyEvent(object sender, TransformPropertyEventArgs e)
        {
            IPageTransferContext pageTransferContext = sender as IPageTransferContext;

            if (string.IsNullOrEmpty(e.PropertySource.TypeName))
                return;

            Type type = Type.GetType(e.PropertySource.TypeName);

            if (pageTransferContext == null || e == null || e.IsHandled || e.PropertySource == null || e.PropertySource.Type != PropertyDataType.LongString ||
                type == null || type.BaseType == null || !type.BaseType.FullName.Contains("ElencySolutions.MultipleProperty.MultiplePropertyBase"))
            {
                return;
            }

            PropertyData instance = Activator.CreateInstance(type) as PropertyData;
            instance.LoadData(e.PropertySource.Value);

            IMirroringHandlerProperty mirroringHandlerProperty = instance as IMirroringHandlerProperty;

            if (mirroringHandlerProperty == null)
                return;

            mirroringHandlerProperty.HandleMirroring(pageTransferContext, e, true);
            e.PropertyDestination.Value = instance.Value.ToString();
            e.IsHandled = true;
        }

        public void Uninitialize(InitializationEngine context)
        {
            MirroringDataImporter.ImportPropertyEvent -= MirroringDataImporterImportPropertyEvent;
            MirroringDataExporter.ExportPropertyEvent -= MirroringDataExporterExportPropertyEvent;
        }

        public void Preload(string[] parameters)
        {
        }
    }
}
